/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package inovacao.modelos;

import inovacao.views.AddingDocumentListenerJTextFieldSample;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.text.BadLocationException;
import javax.swing.text.Document;

/**
 *
 * @author Giovani
 */
public class CustomizadoTextField extends JTextField {

    private int limiteTexto;
    private JTable jt;
    private int row;
    private int column;
    

    public CustomizadoTextField(int limiteTexto, JTable jt, int column) {
        super();
        this.limiteTexto = limiteTexto;
        this.jt = jt;        
        this.column = column;

        this.addKeyListener(new java.awt.event.KeyAdapter() {

            @Override
            public void keyTyped(java.awt.event.KeyEvent evt) {
                jTextFieldKeyType(evt);
            }
        });
        
        this.getDocument().addDocumentListener(documentListener);
    }

    private void jTextFieldKeyType(java.awt.event.KeyEvent evt) {
        if (this.getText().length() >= this.limiteTexto) {
            evt.consume();
        }
    }
    
    
    
    

        DocumentListener documentListener = new DocumentListener() {

            public void changedUpdate(DocumentEvent documentEvent) {
                printIt(documentEvent);
            }

            public void insertUpdate(DocumentEvent documentEvent) {
                printIt(documentEvent);
            }

            public void removeUpdate(DocumentEvent documentEvent) {
                printIt(documentEvent);
            }

            private void printIt(DocumentEvent documentEvent) {
                DocumentEvent.EventType type = documentEvent.getType();
                Document source = documentEvent.getDocument();
                int length = source.getLength();

                try {
                    jt.setValueAt(source.getText(0, length).toString(), jt.getSelectedRows()[0], column);
                } catch (BadLocationException ex) {
                    Logger.getLogger(AddingDocumentListenerJTextFieldSample.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        };
}
